package com.example.mq.mqserver.datacenter;

import com.example.mq.common.MqException;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.core.Message;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * 对硬盘所有数据进行管理
 */
public class DiskDataCenter {
    //这个实例用来管理数据库中的数据
    private DataBaseManager dataBaseManager = new DataBaseManager();
    //这个实例用来管理数据文件中的数据
    private MessageFileManager messageFileManager = new MessageFileManager();


    /**
     * 对初始化进行封装
     */
    public void init() {
        dataBaseManager.init();
        messageFileManager.init();
    }


    /**
     * 对交换机封装
     *
     * @param exchange
     */
    public void insertExchange(Exchange exchange) {
        dataBaseManager.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        dataBaseManager.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchanges() {
        return dataBaseManager.selectAllExchanges();
    }

    /**
     * 对队列进行封装
     *
     * @param queue
     * @throws IOException
     */
    public void insertQueue(MSGQueue queue) throws IOException {
        dataBaseManager.insertQueue(queue);
        //创建队列的同时，需要将队列对象写入到数据库中，还需要创建出对应的目录和文件
        messageFileManager.createQueueFiles(queue.getName());
    }

    public void deleteQueue(String queueName) throws IOException {
        dataBaseManager.deleteQueue(queueName);
        messageFileManager.destroyQueueFiles(queueName);
    }

    public List<MSGQueue> selectAllQueues() {
        return dataBaseManager.selectAllQueues();
    }

    /**
     * 对绑定进行封装
     *
     * @param binding
     */
    public void insertBinding(Binding binding) {
        dataBaseManager.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        dataBaseManager.deleteBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return dataBaseManager.selectAllBindings();
    }


    /**
     * 发送消息
     *
     * @param queue
     * @param message
     * @throws IOException
     * @throws MqException
     */
    public void sendMessage(MSGQueue queue, Message message) throws IOException, MqException {
        messageFileManager.sendMessage(queue, message);
    }


    /**
     * 删除消息
     *
     * @param queue
     * @param message
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws MqException
     */
    public void deleteMessage(MSGQueue queue, Message message) throws IOException, ClassNotFoundException, MqException {
        messageFileManager.deleteMessage(queue, message);
        //删除完后要考虑是否要gc
        if (messageFileManager.checkGC(queue.getName())) {
            messageFileManager.gc(queue);
        }
    }

    /**
     * 查询所有消息
     *
     * @param queueName
     * @return
     * @throws IOException
     * @throws MqException
     * @throws ClassNotFoundException
     */
    public LinkedList<Message> loadAllMessageFromQueue(String queueName) throws IOException, MqException, ClassNotFoundException {
        return messageFileManager.loadAllMessageFromQueue(queueName);
    }


}
